package com.globallogic.threads.executors;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class WorkExecutor {
	private static final int NUM_OF_DATA = 20;
	private static final int ARRAY_SIZE = 1000;
	private static final int POOL_SIZE = 1;
	
	public static void main(String[] args) throws InterruptedException, ExecutionException{
		ExecutorService executorService = Executors.newFixedThreadPool(POOL_SIZE);
		List<Future<Double>> results = null;
		List<Work> works = new ArrayList<>();
		
		for(int i = 0; i < NUM_OF_DATA; i++){
			works.add(new Work(generateArray(ARRAY_SIZE)));
		}
		
		long startTime = System.currentTimeMillis();
		results = executorService.invokeAll(works);
		long endTime = System.currentTimeMillis();
		long duration = (endTime - startTime);
		
		executorService.shutdown();
		System.out.println("Time of execution: " + duration);
		
		printData(results);
		
	}
	
	private static Double[][] generateArray(int size){
		Double[][] data = new Double[size][size];
		
		for(int i=0; i < size; i++){
			for(int j = 0; j < size; j++){
				data[i][j] = Math.random() * 1000;
			}
		}
		
		return data;
	}
	
	private static void printData(List<Future<Double>> data){
		for(Future<Double> res : data){
			try {
				System.out.println("Result is " + res.get()+ " is done: " + res.isDone());
			} catch (InterruptedException | ExecutionException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}
